class Producer {
  constructor(queue) {
    this.queue = queue;
  }

  async create(data) {
    this.queue.push(data);
    console.log(`Produced: ${data}`);
    await new Promise(resolve => setTimeout(resolve, 1000)); // 模拟异步操作
    this.queue.notify(); // 通知队列有新数据
  }
}

class Consumer {
  constructor(queue) {
    this.queue = queue;
  }

  async destroy() {
    if (this.queue.length > 0) {
      const data = this.queue.shift();
      console.log(`Consumed: ${data}`);
      await new Promise(resolve => setTimeout(resolve, 1000)); // 模拟异步操作
    }
  }
}

class Queue {
  constructor() {
    this.items = [];
  }

  push(data) {
    this.items.push(data);
  }

  shift() {
    return this.items.shift();
  }

  get length() {
    return this.items.length;
  }

  async notify() {
    if (this.length > 0) {
      await new Promise(resolve => setTimeout(resolve, 1000)); // 模拟异步操作
      this.consumer.destroy(); // 通知消费者消费数据
    }
  }
}

// 创建队列和生产者消费者实例
const queue = new Queue();
const producer = new Producer(queue);
const consumer = new Consumer(queue);

// 启动生产者和消费者
async function main() {
  producer.create('Initial Data'); // 初始生产一个数据
  consumer.destroy(); // 初始消费一个数据
}

main();